﻿using Microsoft.AspNetCore.Cors.Infrastructure;

namespace CTP.CRM.Core.Cors
{
    public static class CorsPolicyStartupExtensions
    {
        public static IServiceCollection AddCorsPolicy(this IServiceCollection services)
        {
            IConfiguration configuration = services.GetConfiguration();
            List<string> corsPath = configuration.GetSection("CorsPolicy").Get<List<string>>();
            services.AddCors(delegate (CorsOptions options)
            {
                options.AddPolicy("CorsPolicy", delegate (CorsPolicyBuilder builder)
                {
                    builder.WithOrigins(corsPath.ToArray()).AllowAnyMethod().AllowAnyHeader()
                        .AllowCredentials();
                });
            });
            Console.WriteLine("-- 跨域配置:" + string.Join(',', corsPath));
            return services;
        }

        public static IApplicationBuilder UseCorsPolicy(this IApplicationBuilder app)
        {
            app.UseCors("CorsPolicy");
            return app;
        }
    }
}
